Shell: A command-line interpreter that executes commands read from standard input. Bash (Bourne Again SHell) is one of the most common shells.
Terminal: A program that provides a text-based interface to the shell.
Command: An instruction given by a user telling a computer to do something.
Argument: An input given to a command to specify what it should operate on (e.g., a filename).
Flag (or Option): Modifies the behavior of a command (e.g., ls -l, where -l is a flag).
Piping (|): A mechanism to send the output of one command as the input to another command.
Redirection (>, <): A mechanism to change where standard input comes from or where standard output goes.
sudo: "superuser do". A command that allows you to run other commands with administrative (root) privileges.
🧮 Command Structure
Bash commands typically follow this pattern:
command [options] [arguments]
command: The name of the program to run (e.g., ls).
[options]: Start with a dash (-) or two (--). They modify the command's behavior. Multiple short options can often be combined (e.g., ls -la).
[arguments]: The items the command will act upon (e.g., files or directories).
Example:grep -i "error" /var/log/syslog
🛠️ Core Commands Reference
File and Directory Navigation
Command
Description
ls
Lists directory contents. (Use ls -la for detailed, hidden files).
cd [dir]
Changes the current directory. (cd .. moves up one level).
pwd
Prints the current working directory.
File and Directory Management
Command
Description
touch [file]
Creates a new, empty file.
mkdir [dir]
Creates a new directory.
cp [source] [dest]
Copies files or directories. (Use cp -r for directories).
mv [source] [dest]
Moves or renames files or directories.
rm [file]
Removes a file. (Use rm -r for directories; use with caution!).
Viewing and Searching Files
Command
Description
cat [file]
Prints the entire content of a file to the screen.
less [file]
Views file content one page at a time (allows scrolling).
head / tail [file]
Views the first / last 10 lines of a file. (Use tail -f to follow a file live).
grep [pattern] [file]
Searches for a pattern within a file.
find [path] -name [pattern]
Searches for files and directories in a directory hierarchy.
System and Process Management
Command
Description
ps aux
Lists currently running processes.
kill [pid]
Sends a signal to terminate a process by its ID.
df -h
Displays disk space usage in a human-readable format.
chmod
Changes the permissions of a file or directory.
🧭 Common Workflows with Piping
Piping (|) is the key to combining simple tools to perform complex tasks.
Find large files: Find all files in the current directory, get their details, sort by size, and show the top 5.
find . -type f -exec ls -lh {} + | sort -rh -k 5 | head -n 5
Search command history: Find a command you ran previously that involved copying a file.
history | grep "cp"
Count files in a directory: List all files (one per line) and then count the lines.
ls -1 | wc -l
⌨️ Productivity Tips & Shortcuts
Tab: Autocomplete. Press Tab to complete file/directory names or commands. Press it twice to see all possibilities.
Ctrl + R: Reverse Search. Searches your command history as you type.
Ctrl + C: Interrupt. Stops the currently running command.
Ctrl + A / Ctrl + E: Move cursor to the beginning / end of the line.
!!: Re-run the last command. (Use sudo !! to run the last command with sudo).
Aliases: Create shortcuts for long commands in your ~/.bashrc or ~/.zshrc file.
alias ll='ls -la'
alias update='sudo apt update && sudo apt upgrade'
📊 Piping and Redirection Explained
Operator
Name
Example
Description
|
Pipe
ls -l | less
Sends the output of ls -l as the input to less.
>
Redirect Output
ls > file_list.txt
Writes the output of ls to a file, overwriting it if it exists.
>>
Append Output
echo "Error log" >> app.log
Appends the output to the end of a file.
<
Redirect Input
sort < names.txt
Sends the content of names.txt as input to the sort command.
🧪 Practical One-Liner Examples
Find and Delete All .tmp files
find . -name "*.tmp" -type f -delete
Monitor a Log File for Errors in Real-Time
tail -f /var/log/nginx/access.log | grep " 404 "
Create a Quick Backup of a File
cp config.yaml{,.bak}
# Expands to: cp config.yaml config.yaml.bak
🧹 Troubleshooting Common Issues
Error: command not found
Fix: The program is not installed, or its location is not in your system's $PATH. Check for typos. Use which [command] to see if it's installed.
Error: Permission denied
Fix: You do not have the necessary permissions to read, write, or execute the file/directory. Check permissions with ls -l. You may need to use sudo for administrative tasks.
Problem: A command is stuck or running too long.
Fix: Press Ctrl + C to send an interrupt signal and terminate the process.
Problem: Accidentally deleted a file with rm.
Fix: In most cases, this is irreversible on the command line. There is no trash can. Always double-check your rm -r commands. Some users alias rm to rm -i (interactive mode) for safety.